<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.example.dao.ProductDao" %>
<%@ page import="com.example.util.DBUtil" %>
<%@ page import="com.example.domain.Product" %>
<%@ page import="com.example.domain.User" %>
<%@ page import="java.util.List" %>
<%@ page import="java.sql.Connection" %>
<html>
<head>
    <title>商品列表 - 购物网</title>
    <link rel="stylesheet" href="../assets/css/styles.css">
    <style>
        /* 主体内容布局 */
        .content-wrapper {
            display: flex;
            gap: 20px;
            margin-top: 20px;
        }
        
        /* 筛选侧边栏 */
        .filter-sidebar {
            width: 250px;
            background: var(--white-color);
            border-radius: var(--border-radius-lg);
            padding: 20px;
            box-shadow: var(--shadow);
            height: fit-content;
            transition: var(--transition);
        }
        
        .filter-sidebar:hover {
            box-shadow: var(--shadow-md);
        }
        
        .filter-sidebar h3 {
            margin-bottom: 15px;
            padding-bottom: 10px;
            border-bottom: 1px solid var(--border-color);
            color: var(--gray-800);
            font-size: 1.1rem;
            font-weight: 600;
        }
        
        .filter-section {
            margin-bottom: 25px;
        }
        
        .filter-section h4 {
            margin: 0 0 15px 0;
            color: var(--gray-700);
            font-size: 0.95rem;
            font-weight: 600;
        }
        
        .price-range {
            display: flex;
            gap: 10px;
            margin-bottom: 15px;
            align-items: center;
        }
        
        .price-input {
            flex: 1;
            padding: 8px 12px;
            border: 1px solid var(--gray-300);
            border-radius: var(--border-radius);
            font-size: 0.9rem;
            transition: var(--transition);
        }
        
        .price-input:focus {
            outline: none;
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(66, 99, 235, 0.1);
        }
        
        .price-separator {
            color: var(--gray-500);
            font-weight: 600;
        }
        
        .checkbox-item {
            margin-bottom: 12px;
            position: relative;
            padding-left: 28px;
            cursor: pointer;
            user-select: none;
            display: block;
            color: var(--gray-700);
            transition: var(--transition);
        }
        
        .checkbox-item:hover {
            color: var(--primary-color);
        }
        
        .checkbox-item input {
            position: absolute;
            opacity: 0;
            cursor: pointer;
            height: 0;
            width: 0;
        }
        
        .checkmark {
            position: absolute;
            top: 2px;
            left: 0;
            height: 18px;
            width: 18px;
            background-color: var(--gray-100);
            border: 1px solid var(--gray-300);
            border-radius: var(--border-radius);
            transition: var(--transition);
        }
        
        .checkbox-item:hover input ~ .checkmark {
            background-color: var(--gray-200);
            border-color: var(--primary-color);
        }
        
        .checkbox-item input:checked ~ .checkmark {
            background-color: var(--primary-color);
            border-color: var(--primary-color);
        }
        
        .checkmark:after {
            content: "";
            position: absolute;
            display: none;
            left: 6px;
            top: 3px;
            width: 5px;
            height: 10px;
            border: solid white;
            border-width: 0 2px 2px 0;
            transform: rotate(45deg);
        }
        
        .checkbox-item input:checked ~ .checkmark:after {
            display: block;
        }
        
        /* 主内容区域 */
        .main-content {
            flex: 1;
            min-width: 300px;
        }
        
        /* 页面头部 */
        .page-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding: 15px 0;
            border-bottom: 1px solid var(--border-color);
        }
        
        .page-header h1 {
            margin: 0;
            color: var(--gray-800);
            font-size: 1.75rem;
            font-weight: 600;
        }
        
        /* 排序选项 */
        .sort-options {
            display: flex;
            gap: 8px;
            background: var(--gray-50);
            padding: 5px;
            border-radius: var(--border-radius-lg);
        }
        
        .sort-option {
            padding: 6px 15px;
            border: 1px solid transparent;
            border-radius: var(--border-radius);
            cursor: pointer;
            background: transparent;
            color: var(--gray-700);
            font-size: 0.9rem;
            transition: var(--transition);
            user-select: none;
        }
        
        .sort-option:hover {
            background: var(--gray-100);
            border-color: var(--gray-200);
        }
        
        .sort-option.active {
            background: var(--primary-color);
            color: white;
            border-color: var(--primary-color);
        }
        
        /* 商品网格 */
        .product-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
            gap: 20px;
        }
        
        /* 商品卡片增强 */
        .product-card {
            background: var(--white-color);
            border-radius: var(--border-radius-lg);
            overflow: hidden;
            box-shadow: var(--shadow);
            transition: var(--transition);
            display: flex;
            flex-direction: column;
        }
        
        .product-card:hover {
            transform: translateY(-5px);
            box-shadow: var(--shadow-lg);
        }
        
        .product-image {
            height: 200px;
            overflow: hidden;
            display: flex;
            align-items: center;
            justify-content: center;
            background: var(--gray-50);
            position: relative;
        }
        
        .product-image img {
            width: 100%;
            height: 100%;
            object-fit: contain;
            transition: var(--transition);
            padding: 10px;
        }
        
        .product-card:hover .product-image img {
            transform: scale(1.05);
        }
        
        .product-badge {
            position: absolute;
            top: 10px;
            left: 10px;
            background: var(--primary-color);
            color: white;
            padding: 4px 8px;
            border-radius: var(--border-radius);
            font-size: 0.8rem;
            font-weight: 600;
        }
        
        .product-info {
            padding: 15px;
            flex: 1;
            display: flex;
            flex-direction: column;
        }
        
        .product-name {
            font-size: 1.1rem;
            font-weight: 600;
            margin-bottom: 8px;
            color: var(--gray-800);
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 1;
            -webkit-box-orient: vertical;
        }
        
        .product-intro {
            font-size: 0.9rem;
            color: var(--gray-600);
            margin-bottom: 12px;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            line-height: 1.4;
        }
        
        .product-price {
            font-size: 1.25rem;
            font-weight: 700;
            color: var(--primary-color);
            margin-bottom: 10px;
        }
        
        .original-price {
            font-size: 0.9rem;
            color: var(--gray-400);
            text-decoration: line-through;
            margin-left: 5px;
            font-weight: normal;
        }
        
        .product-stats {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 12px;
            font-size: 0.85rem;
            color: var(--gray-500);
        }
        
        .product-actions {
            margin-top: auto;
        }
        
        .btn-cart {
            width: 100%;
            padding: 8px 16px;
            border: 1px solid var(--primary-color);
            border-radius: var(--border-radius);
            background: var(--white-color);
            color: var(--primary-color);
            font-size: 0.9rem;
            font-weight: 500;
            cursor: pointer;
            transition: var(--transition);
        }
        
        .btn-cart:hover {
            background: var(--primary-color);
            color: white;
        }
        
        .btn-cart.added {
            background: var(--success-color);
            border-color: var(--success-color);
            color: white;
        }
        
        /* 加载动画 */
        .loading-spinner {
            display: none;
            border: 3px solid var(--gray-200);
            border-top: 3px solid var(--primary-color);
            border-radius: 50%;
            width: 20px;
            height: 20px;
            animation: spin 1s linear infinite;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        /* 响应式设计 */
        @media (max-width: 1024px) {
            .filter-sidebar {
                width: 220px;
            }
            
            .product-grid {
                grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
            }
        }
        
        @media (max-width: 768px) {
            .content-wrapper {
                flex-direction: column;
            }
            
            .filter-sidebar {
                width: 100%;
                margin-bottom: 20px;
            }
            
            .page-header {
                flex-direction: column;
                align-items: flex-start;
                gap: 15px;
            }
            
            .sort-options {
                width: 100%;
                justify-content: space-between;
                overflow-x: auto;
                padding: 8px;
                -webkit-overflow-scrolling: touch;
            }
            
            .sort-option {
                white-space: nowrap;
                flex-shrink: 0;
            }
            
            .product-grid {
                grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
                gap: 15px;
            }
            
            .product-image {
                height: 150px;
            }
        }
    </style>
</head>
<body>
    <% 
        // 检查用户是否已登录
        com.example.domain.User currentUser = (com.example.domain.User) session.getAttribute("user");
    %>
    
    <!-- 导航栏 -->
    <nav class="header-nav">
        <div class="nav-container">
            <a href="../index.jsp" class="nav-brand">
                🛒 购物网
            </a>
            
            <!-- 搜索框 -->
            <div style="flex: 1; max-width: 400px; margin: 0 20px; position: relative;">
                <input type="text" placeholder="搜索商品..." style="width: 100%; padding: 10px 15px; padding-right: 40px; border-radius: var(--border-radius); border: 1px solid var(--gray-300); transition: var(--transition);">
                <button style="position: absolute; right: 10px; top: 50%; transform: translateY(-50%); background: none; border: none; color: var(--gray-500); cursor: pointer; font-size: 1.2rem;">
                    🔍
                </button>
            </div>
            
            <ul class="nav-links">
                <li><a href="../index.jsp">首页</a></li>
                <li><a href="list.jsp" class="active">商品浏览</a></li>
                <li><a href="../cart/view/cart.jsp">购物车</a></li>
                <% if (currentUser != null) { %>
                    <li><a href="../admin/user_info.jsp">用户中心</a></li>
                <% } %>
            </ul>
            
            <div class="user-actions">
                <% if (currentUser != null) { %>
                    <span class="user-info">欢迎, <%= currentUser.getUsername() %>!</span>
                    <a href="../auth/logout.jsp" class="btn btn-outline">退出</a>
                <% } else { %>
                    <a href="../auth/login.jsp" class="btn btn-outline">登录</a>
                    <a href="../auth/register.jsp" class="btn" style="margin-left: 10px;">注册</a>
                <% } %>
            </div>
        </div>
    </nav>
    
    <div class="container">
        <div class="page-header">
            <h1>全部商品</h1>
            <div class="sort-options">
                <div class="sort-option active">综合排序</div>
                <div class="sort-option">销量优先</div>
                <div class="sort-option">价格从低到高</div>
                <div class="sort-option">价格从高到低</div>
                <div class="sort-option">新品优先</div>
            </div>
        </div>
        
        <div class="content-wrapper">
            <!-- 侧边栏筛选 -->
            <div class="filter-sidebar">
                <h3>商品筛选</h3>
                
                <div class="filter-section">
                    <h4>价格区间</h4>
                    <div class="price-range">
                        <input type="text" placeholder="最低价" class="price-input">
                        <span class="price-separator">-</span>
                        <input type="text" placeholder="最高价" class="price-input">
                    </div>
                    <button class="btn btn-primary" style="width: 100%;">应用筛选</button>
                </div>
                
                <div class="filter-section">
                    <h4>商品分类</h4>
                    <label class="checkbox-item">
                        <input type="checkbox">
                        <span class="checkmark"></span>
                        手机数码
                    </label>
                    <label class="checkbox-item">
                        <input type="checkbox">
                        <span class="checkmark"></span>
                        服装鞋包
                    </label>
                    <label class="checkbox-item">
                        <input type="checkbox">
                        <span class="checkmark"></span>
                        电脑办公
                    </label>
                    <label class="checkbox-item">
                        <input type="checkbox">
                        <span class="checkmark"></span>
                        家居用品
                    </label>
                    <label class="checkbox-item">
                        <input type="checkbox">
                        <span class="checkmark"></span>
                        美妆护肤
                    </label>
                </div>
                
                <div class="filter-section">
                    <h4>其他筛选</h4>
                    <label class="checkbox-item">
                        <input type="checkbox">
                        <span class="checkmark"></span>
                        有促销活动
                    </label>
                    <label class="checkbox-item">
                        <input type="checkbox">
                        <span class="checkmark"></span>
                        可分期付款
                    </label>
                    <label class="checkbox-item">
                        <input type="checkbox">
                        <span class="checkmark"></span>
                        免费配送
                    </label>
                </div>
            </div>
            
            <!-- 商品列表 -->
            <div class="main-content">
                <div class="product-grid">
                    <% 
                        Connection conn = null;
                        try {
                            conn = DBUtil.getConnection();
                            ProductDao productDao = new ProductDao();
                            List<Product> products = productDao.getAllProducts();
                            
                            if (products.isEmpty()) {
                    %>
                        <div class="card" style="padding: 2rem; text-align: center; grid-column: span 4;">
                            <p>暂无商品</p>
                        </div>
                    <% 
                            } else {
                                for (Product product : products) {
                    %>
                    <div class="product-card fade-in">
                        <div class="product-image">
                            <% if (product.getSale_price() > 0) { %>
                                <span class="product-badge">特价</span>
                            <% } %>
                            <% if (product.getLogo() != null && !product.getLogo().isEmpty()) { %>
                                <img src="<%= request.getContextPath() + "/" + product.getLogo() %>" alt="<%= product.getProduct_name() %>">
                            <% } else { %>
                                <img src="<%= request.getContextPath() %>/assets/images/no-image.svg" alt="暂无图片" style="opacity: 0.5;">
                            <% } %>
                        </div>
                        <div class="product-info">
                            <div class="product-name"><%= product.getProduct_name() %></div>
                            <div class="product-intro"><%= product.getIntro() != null ? product.getIntro() : "暂无介绍" %></div>
                            <div class="product-price">
                                <% if (product.getSale_price() > 0) { %>
                                    ¥<%= product.getSale_price() %>
                                    <span class="original-price">¥<%= product.getPrice() %></span>
                                <% } else { %>
                                    ¥<%= product.getPrice() %>
                                <% } %>
                            </div>
                            <div class="product-stats">
                                <span>销量: 1000+</span>
                                <span>评价: 5.0</span>
                            </div>
                            <div class="product-actions">
                                <button class="btn-cart" onclick="addToCart(<%= product.getId() %>, this)">
                                    加入购物车
                                    <span class="loading-spinner"></span>
                                </button>
                            </div>
                        </div>
                    </div>
                    <% 
                                }
                            }
                        } catch (Exception e) {
                    %>
                        <div class="card" style="padding: 2rem; text-align: center; grid-column: span 4;">
                            <p>加载商品失败: <%= e.getMessage() %></p>
                        </div>
                    <% 
                        } finally {
                            if (conn != null) {
                                try {
                                    conn.close();
                                } catch (Exception e) {
                                    // 忽略关闭连接的异常
                                }
                            }
                        }
                    %>
                </div>
            </div>
        </div>
    </div>
    
    <!-- 页脚 -->
    <footer class="footer">
        <div class="container">
            <div class="footer-top">
                <div>
                    <a href="../index.jsp" class="footer-logo">🛒 购物网</a>
                    <p style="margin-top: 1rem; color: var(--gray-400); font-size: 0.9rem;">
                        优质商品，优质服务，打造您的理想购物体验。
                    </p>
                </div>
                
                <div>
                    <h4 class="footer-heading">快速链接</h4>
                    <ul class="footer-links">
                        <li><a href="../index.jsp">首页</a></li>
                        <li><a href="list.jsp">商品浏览</a></li>
                        <li><a href="#">关于我们</a></li>
                        <li><a href="#">联系我们</a></li>
                    </ul>
                </div>
                
                <div>
                    <h4 class="footer-heading">客户服务</h4>
                    <ul class="footer-links">
                        <li><a href="#">帮助中心</a></li>
                        <li><a href="#">售后政策</a></li>
                        <li><a href="#">配送信息</a></li>
                        <li><a href="#">常见问题</a></li>
                    </ul>
                </div>
            </div>
            
            <div class="footer-bottom">
                <p>&copy; 2025 购物网. 保留所有权利.</p>
            </div>
        </div>
    </footer>
    
    <script>
        function addToCart(productId, button) {
            // 检查用户是否已登录
            <% if (session.getAttribute("user") == null) { %>
                if (confirm("您需要先登录才能添加商品到购物车，是否前往登录？")) {
                    window.location.href = '../auth/login.jsp';
                }
                return;
            <% } %>
            
            // 获取按钮和加载指示器
            const spinner = button.querySelector('.loading-spinner');
            const originalText = button.textContent;
            
            // 禁用按钮并显示加载状态
            button.disabled = true;
            spinner.style.display = 'inline-block';
            button.innerHTML = '<span class="loading-spinner"></span> 正在添加...';
            
            // 发送 AJAX 请求添加到购物车
            const xhr = new XMLHttpRequest();
            xhr.open('POST', '../cart/api/add.jsp', true);
            xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4) {
                    // 恢复按钮状态
                    button.disabled = false;
                    spinner.style.display = 'none';
                    
                    if (xhr.status === 200) {
                        if (xhr.responseText === 'success') {
                            // 更新按钮状态
                            button.textContent = '已添加';
                            button.classList.add('added');
                            
                            // 显示成功提示
                            showNotification('商品已添加到购物车！');
                            
                            // 2秒后恢复按钮
                            setTimeout(function() {
                                button.textContent = '加入购物车';
                                button.classList.remove('added');
                            }, 2000);
                        } else {
                            button.textContent = originalText;
                            showNotification('添加失败: ' + xhr.responseText, 'error');
                        }
                    } else {
                        button.textContent = originalText;
                        showNotification('网络错误，请稍后重试', 'error');
                    }
                }
            };
            xhr.send('productId=' + productId + '&quantity=1');
        }
        
        // 显示通知
        function showNotification(message, type = 'success') {
            // 创建通知元素
            const notification = document.createElement('div');
            notification.className = `notification ${type}`;
            notification.textContent = message;
            notification.style.cssText = `
                position: fixed;
                top: 20px;
                right: 20px;
                padding: 12px 20px;
                border-radius: var(--border-radius);
                color: white;
                font-weight: 500;
                z-index: 1000;
                animation: slideInRight 0.3s ease-out;
                background: ${type === 'success' ? 'var(--success-color)' : 'var(--danger-color)'};
            `;
            
            // 添加到文档
            document.body.appendChild(notification);
            
            // 3秒后移除
            setTimeout(() => {
                notification.style.animation = 'slideOutRight 0.3s ease-in';
                setTimeout(() => {
                    document.body.removeChild(notification);
                }, 300);
            }, 3000);
        }
        
        // 排序选项点击事件
        document.querySelectorAll('.sort-option').forEach(option => {
            option.addEventListener('click', function() {
                // 移除所有活动状态
                document.querySelectorAll('.sort-option').forEach(opt => opt.classList.remove('active'));
                // 添加当前活动状态
                this.classList.add('active');
            });
        });
    </script>
</body>
</html>